home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / telecom / 133 / c / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-04-17  |  5.1 KB  |  104 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /* Module:     getopt.c - Get option letter from argument vector            */
  4. /*                                                                          */
  5. /* Programmer: Uncertain. From USENET, modified by Jawhar Bammi and         */
  6. /*             George R. Woodside.                                          */
  7. /*                                                                          */
  8. /* Date:       December 11, 1986                                            */
  9. /*                                                                          */
  10. /* Function:   Parses options in a command line, according to               */
  11. /*             an input parameter string.                                   */
  12. /*                                                                          */
  13. /*             The input string should be a list of flags to parse for.     */
  14. /*             Flags followed by a colon require an extra option value.     */
  15. /*             Parsing stops when an argument with no leading dash is       */
  16. /*             encountered.                                                 */
  17. /*                                                                          */
  18. /* Returns:    Each detected flag. Normally, these will be processed in     */
  19. /*             the calling program by a switch statement. If the flag had   */
  20. /*             a following colon, the option argument is identified by      */
  21. /*             the char pointer optarg. If an unidentified flag is          */
  22. /*             encountered, getopt prints an error message. When an         */
  23. /*             argument with no leading hyphen terminates parsing, the      */
  24. /*             array index of that argument is left in optind.              */
  25. /*                                                                          */
  26. /* Sample:     while( (c=getopt(argc,argv,"ABabC:D:c:d:")) != EOF)          */
  27. /*               {                                                          */
  28. /*                 switch(c)                                                */
  29. /*                   {                                                      */
  30. /*                     case :                                               */
  31. /*                                                                          */
  32. /*                                                                          */
  33. /*             A command line of "prog -a -c foo extra" would then return   */
  34. /*             cases of a and c, with c having optarg foo. optind would     */
  35. /*             have a value of 4, indicating that argv[4]  ("extra")        */
  36. /*             was not processed.                                           */
  37. /*                                                                          */
  38. /****************************************************************************/
  39.  
  40. #include <stdio.h>
  41.  
  42. #define BADCH   (int)'?'
  43. #define EMSG    ""
  44. #define tell(s) printf((char *)*nargv); \
  45.                 printf((char *)s);      \
  46.                 printf("%c\n",optopt);  \
  47.                 return(BADCH);
  48.  
  49.  
  50. char    *optarg;                        /* argument to return               */
  51.  
  52. int     optind = 1;                     /* index to argv vector             */
  53. int     optopt;                         /* character checked                */
  54.  
  55. int     getopt(nargc,nargv,ostr)
  56. int     nargc;
  57. char    **nargv;
  58. char    *ostr;
  59.  
  60. {
  61.   static   char  *place = EMSG;         /* option letter processing         */
  62.   register char  *oli;                  /* option letter list index         */
  63.   extern   char  *index();
  64.  
  65.   if(!*place) 
  66.     {                                   /* update scanning pointer          */
  67.       if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place)
  68.         return(EOF);
  69.  
  70.       if (*place == '-')
  71.         {                               /* found "--"                       */
  72.           ++optind;
  73.           return(EOF);
  74.         }
  75.     }                                   /* option letter okay?              */
  76.  
  77.   if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr,optopt)))
  78.     {
  79.       if(!*place) ++optind;
  80.       tell(": illegal option -- ");
  81.     }
  82.  
  83.   if (*++oli != ':')
  84.     {                                   /* don't need argument              */
  85.       optarg = (char *)NULL;
  86.       if (!*place) ++optind;
  87.     }
  88.   else
  89.     {                                   /* need an argument                 */
  90.       if (*place)
  91.         optarg = place;                 /* no white space                   */
  92.       else if (nargc <= ++optind)
  93.         {                               /* no arg                           */
  94.           place = EMSG;
  95.           tell(": option requires an argument -- ");
  96.         }
  97.       else
  98.         optarg = nargv[optind];         /* white space                      */
  99.       place = EMSG;
  100.       ++optind;
  101.     }
  102.   return(optopt);                       /* dump back option letter          */
  103. }
  104.